package com.ctgu.ImageRecognition.controller;


import com.ctgu.ImageRecognition.entity.Photo;
import com.ctgu.ImageRecognition.service.PhotoService;
import com.ctgu.ImageRecognition.util.DetectLabelProUtils;
import com.ctgu.ImageRecognition.util.GsonUtils;
import com.ctgu.ImageRecognition.util.QiniuUploadUtils;
import com.ctgu.ImageRecognition.util.ResultUtil;
import com.ctgu.ImageRecognition.vo.Labels;
import com.ctgu.ImageRecognition.vo.ResultVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Photo)表控制层
 *
 * @author 
 * @since 2023-07-05 18:31:00
 */
@RestController
@RequestMapping("/photo")
public class PhotoController {
    /**
     * 服务对象
     */
    @Resource
    private PhotoService photoService;

    /**
    * Description:测试上传照片
    * @author: 吴杰
    * @date: 2023/7/10 15:42
    * @param:
     * * @return:
    */
    @RequestMapping("/test")
    public ResultVO<String> test(MultipartFile file ){
        System.out.println("file = " + file);
        String Filename = file.getOriginalFilename();
        System.out.println("Filename = " + Filename);
        return QiniuUploadUtils.uploadImage(file);
    }

    /**
    * Description: 查询出所有图片
    * @author: 吴杰
    * @date: 2023/7/11 10:34
    * @param:
     * * @return:
    */
    @RequestMapping("/list")
    public ResultVO listPhoto(){
        return ResultUtil.success("查询成功",photoService.listPhoto());
    }


/**
* Description:更新照片名字或者收藏状态
* @author: 吴杰
* @date: 2023/7/11 16:09
* @param:
 * * @return:
*/
    @RequestMapping("/updatePhoto")
    public ResultVO updatePhoto(@RequestBody Photo photo){
    int result = photoService.updatePhoto(photo);
    if(result<=0){
        return ResultUtil.fail("未修改成功或者数据无更改",null);
    }

    return ResultUtil.success("修改成功,返回值为修改了几条数据",result);

    }
//
//    /**
//    * Description:删除图片
//    * @author: 吴杰
//    * @date: 2023/7/12 16:26
//    * @param:
//     * * @return:
//    */
//    @RequestMapping("/delete")
//    public ResultVO deletePhoto(int id){
//        int result = photoService.deletePhoto(id);
//        if(result<=0){
//            return ResultUtil.fail("未修改成功或者未找到可删除的数据",null);
//        }
//        return ResultUtil.success("修改成功,返回值为修改了几条数据",result);
//    }


    @RequestMapping("/collect")
    public ResultVO collectPhoto(int pId){
        int result = photoService.collectPhoto(pId);
        if(result<=0){
            return ResultUtil.fail("改变收藏状态失败",null);
        }
        return ResultUtil.success("改变收藏状态成功",result);
    }



    /**
     * Description:上传照片
     * @author: 熊道财
     * @date: 2023/7/10 15:42
     * @param:
     * * @return:
     */

    @RequestMapping("/upload")
    @ResponseBody
    public ResultVO<String> upload( MultipartFile file){
        System.out.println("file = " + file);
        String Filename = file.getOriginalFilename();
        System.out.println("Filename = " + Filename);
        ResultVO resultVO=QiniuUploadUtils.uploadImage(file);
        System.out.println(resultVO);
        return resultVO;
    }

    /**
     * Description:删除照片
     * @author: 熊道财
     * @date: 2023/7/12 15:42
     * @param:
     * * @return:
     */
    @RequestMapping("/delete")
    public ResultVO<String> delete(String key){
        System.out.println("key = " + key);
        photoService.deletePhoto(key);
        ResultVO resultVO=QiniuUploadUtils.delete(key);
        return resultVO;
    }
    /**
     * Description:识别图片
     * @author: 熊道财
     * @date: 2023/7/12 15:42
     * @param:
     * * @return:
     */
    @RequestMapping("/identify")
    public ResultVO<String> identify(String imageUrl,int uId) {
        ResultVO resultVO = DetectLabelProUtils.identify(imageUrl);
        System.out.println("UID为："+uId);
        Labels label= GsonUtils.fromJson(resultVO.getMsg());
        System.out.println(resultVO.getMsg());
        System.out.println("识别结果 ==>"+label.getName() + "\t" + label.getConfidence() +
                "\t" + label.getFirstCategory() + "\t" + label.getSecondCategory());
        photoService.insertPhoto(imageUrl, label, uId);

        return resultVO;
    }


    /**
    * Description:展示出已收藏的图片
    * @author: 吴杰
    * @date: 2023/7/18 15:25
    * @param:
     * * @return:
    */
    @RequestMapping("collected")
    public List<Photo> listCollection(){
        return photoService.queryCollection();
    }

}

